package com.demo.randomdining.dao

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.demo.randomdining.model.TargetInfo

/**
 * Your class notes
 *
 * @author gaoguanchao
 * @since 2023/10/24 0024
 */
@Database(entities = [TargetInfo::class], version = 1, exportSchema = false)
abstract class TargetDB : RoomDatabase() {

    abstract val targetDao: TargetDao

    companion object {
        @Volatile
        private var INSTANCE: TargetDB? = null
        fun getInstance(context: Context): TargetDB {
            synchronized(this) {
                var instance = INSTANCE
                if (instance == null) {
                    instance = Room.databaseBuilder(
                        context.applicationContext,
                        TargetDB::class.java,
                        "target_database"
                    )
                        .fallbackToDestructiveMigration()
                        .allowMainThreadQueries()
                        .build()
                    INSTANCE = instance
                }
                return instance
            }
        }
    }
}